package com.zp.strategy;

import cn.hutool.core.util.RandomUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * redis 缓存策略
 */
//@Component
@RequiredArgsConstructor
@Primary
public class RedisStrategy implements CacheStrategy {
    private final StringRedisTemplate redisTemplate;

    @Override
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void set(String key, String value, int expireTime) {
        int timeout = RandomUtil.randomInt(0, 70);
        redisTemplate.opsForValue().set(key, value, expireTime + timeout, TimeUnit.SECONDS);
    }

    @Override
    public void set(String key, String value) {

    }

    @Override
    public void delete(String key) {
        redisTemplate.delete(key);
    }

}
